package com.test.vertx.resources;

import com.test.vertx.service.UserService;

import io.vertx.core.CompositeFuture;
import io.vertx.core.Future;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;

public class UserResource {
	
	private Router router;
	
	private UserService userService = new UserService();
	
	public UserResource(Router router, Vertx vertx) {
		this.router = router;
		
		JsonObject result = new JsonObject();
		result.put("name", "luqiaolong");
		result.put("age", 20);
		result.put("sex", "male");
		
		router.get("/users/:id").handler(routingContext -> {
			routingContext.request();
			HttpServerResponse response = routingContext.response();
			result.put("HttpMethod", "GET");
			HttpClient httpClient = vertx.createHttpClient(new HttpClientOptions());
			httpClient.getAbs("http://localhost:8080/users/21", rest -> {
				rest.bodyHandler(buffer -> {
					response.end(buffer.toJsonObject().toString());
				});
				
			});
			
		});
		
		router.post("/users").handler(routingContext -> {
			HttpServerResponse response = routingContext.response();
			result.put("HttpMethod", "post");
			response.end(result.toString());
		});

		router.put("/users/:id").handler(routingContext -> {
			HttpServerResponse response = routingContext.response();
			result.put("HttpMethod", "put");
			response.end(result.toString());
		});
		
		router.delete("/users/:id").handler(routingContext -> {
			HttpServerResponse response = routingContext.response();
			result.put("HttpMethod", "delete");
			response.end(result.toString());
		});
	}
	
}
